<?php
declare (strict_types=1);

namespace app\third\logic;

use EasyWeChat\Factory;
use EasyWeChat\MiniProgram\Application;

/**
 * 微信小程序逻辑
 * Class MiniLogic
 * @package app\third\logic
 */
class MiniLogic
{
    protected static $sdk;

    /**
     * @return Application
     * @throws \Exception
     */
    public static function getSdk()
    {
        if (static::$sdk) {
            return static::$sdk;
        }
        try {
            $miniConfig = thinkConfigs('weixin_mini.*');
            $config = [
                'app_id' => $miniConfig['mini_appid'],
                'secret' => $miniConfig['mini_app_secret'],
                'response_type' => 'array',
            ];
            return static::$sdk = Factory::miniProgram($config);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    public static function code(string $code)
    {
        return static::getSdk()->auth->session($code);
    }
}